package program_structure;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/8/18 22:39
 * while 循环的使用 - 明确循环的终止条件，循环次数不确定
 * break    跳出本次循环
 * continue 跳过本轮循环，执行下一轮循环；
 */
public class WhileDemo {
    public static void main(String[] args) {
        //1.顺序打印1-10的数字
        int num = 1;
        while (num <= 10) {
            System.out.println(num++);
        }
        //2.输出10以内的偶数
        num = 0;
        while (num <= 10) {
            if (num % 2 == 0) {
                System.out.println(num);
                num += 2;
            } else {
                num++;
            }
        }
        //3.计算1-100的和
        num = 0;
        int sum = 0;
        while (num <= 100) {
            sum += num;
            num++;
        }
        System.out.println("1-100的和为：" + sum);

        //4.1000-2022 闰年
        int year = 1000;
        int count = 0;
        while (year < 2023) {
            if (year % 100 == 0) {
                if (year % 400 == 0) {
                    count++;
                    System.out.print(year + ",");
                }
            } else {
                if (year % 4 == 0) {
                    count++;
                    System.out.print(year + ",");
                }
            }
            year++;
        }
        System.out.println("1000-2022的闰年个数为:" + count);

        //5.计算5的阶乘
        num = 1;
        sum = 1;
        while (num <= 5) {
            sum *= num;
            num++;
        }
        System.out.println("5的阶乘为：" + sum);
        //6.计算1！+2！+3！+4！+5！
        num = 1;
        int tarSum = 0;
        while (num <= 5) {
            count = 1;
            sum = 1;
            while (count <= num) {
                sum *= count;
                count++;
            }
            num++;
            tarSum += sum;

        }
        System.out.println("1！+2！+3！+4！+5！为：" + tarSum);
        //7.分号的注意事项
        int number = 1;
//        while(number<=10);{//死循环-------->
//            System.out.println("程序结束");
//        }
        //8.break使用 找出100-200中第一个被3整除的整数
        num = 100;
        while(num<=200){
            if(num%3==0){
                System.out.println(num);
                break;
            }
            num++;
        }
        //9.continue使用 找出100-200中所有能被3整除的整数
        num=100;
        while(num<=200){
            if(num%3!=0){
                num++;
                continue;
            }
            System.out.print(num+",");
            num++;
        }
    }
}

